package com.sgb.kjwl.utils;

import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 * @author: 杨畔
 * @date: 4/19/21 10:25 AM
 * @Description: 手势帮助类
 */
public class TouchHelper {
    //点击x位置
    private float mRowX;
    //点击y位置
    private float mRowY;
    //手势回调
    private TouchListener mListener;

    public static TouchHelper newInstance() {
        return new TouchHelper();
    }

    public TouchHelper addTouchListener(TouchListener listener) {
        mListener = listener;
        return this;
    }

    public TouchHelper onTouch(View view, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mRowX = event.getRawX();
                mRowY = event.getRawY();
                break;
            case MotionEvent.ACTION_UP:
                float upX = event.getRawX();
                float upY = event.getRawY();
                handlerUpTouch(upX, upY);
                break;
        }
        return this;
    }

    /**
     * 处理抬起手势
     *
     * @param upX 抬起时x轴位置
     * @param upY 抬起时y轴位置
     */
    private void handlerUpTouch(float upX, float upY) {
        Log.e("workbench", String.format("downX:%s,downY:%s,upX:%s,upY:%s", mRowX, mRowY, upX, upY));
        if (mListener == null) return;
        //横向滑动距离大于200px，且纵向滑动小于50px则触发滑动手势回调
        if (upX - mRowX > 200 && Math.abs(upY - mRowY) < 50) {
            mListener.rightScroll();
        }
        mListener.handlerUpTouch(mRowX, mRowY, upX, upY);
    }

    public interface TouchListener {
        //抬起时手势位置
        void handlerUpTouch(float downX, float downY, float upX, float upY);

        //右滑
        void rightScroll();
    }

    public static class TouchListenerProxy implements TouchListener {

        @Override
        public void handlerUpTouch(float downX, float downY, float upX, float upY) {

        }

        @Override
        public void rightScroll() {

        }
    }
}
